package fylder.fish;

import android.animation.Animator;
import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.animation.ValueAnimator;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.animation.AccelerateDecelerateInterpolator;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import butterknife.Bind;
import butterknife.ButterKnife;
import fylder.fish.utils.AnimatorPath;
import fylder.fish.utils.DisplayUtils;
import fylder.fish.utils.PathEvaluator;
import fylder.fish.utils.PathPoint;

public class DemoActivity extends AppCompatActivity {

    @Bind(R.id.xlistview_id_footer)
    RelativeLayout fishLay;
    @Bind(R.id.xlistview_id_shark)
    ImageView eatFish;
    @Bind(R.id.xlistview_id_fish)
    ImageView fish1;
    @Bind(R.id.xlistview_id_fish2)
    ImageView fish2;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_demo);
        ButterKnife.bind(this);
        eatFish.post(new Runnable() {
            @Override
            public void run() {
                eatStart();
                fishLay.setVisibility(View.VISIBLE);
            }
        });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mAnimatorSet.end();
    }

    AnimatorSet mAnimatorSet;
    boolean isStart = true;

    void eatStart() {

        DisplayMetrics dm = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(dm);
        int screenWidth = dm.widthPixels;
        final int xStart = (int) eatFish.getX();
        final int xEnd = screenWidth - eatFish.getWidth() - DisplayUtils.dp2px(this, 20);

        ObjectAnimator eatFishAnim = ObjectAnimator.ofFloat(eatFish, "x", xStart, xEnd);
        eatFishAnim.setRepeatCount(ValueAnimator.INFINITE);
        eatFishAnim.setRepeatMode(ValueAnimator.REVERSE);
        eatFishAnim.setDuration(2000);
        eatFishAnim.setInterpolator(new AccelerateDecelerateInterpolator());

        eatFishAnim.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                float value = (float) animation.getAnimatedValue();
                int xt = (int) value;
                if (xt > xEnd - 2) {
                    if (isStart) {
                        eatFish.setImageResource(R.drawable.xlistview_loading_shark2);
                        isStart = false;
                        fishMove(fish2, false);
                    }
                } else if (xt < xStart + 2) {
                    if (!isStart) {
                        eatFish.setImageResource(R.drawable.xlistview_loading_shark);
                        isStart = true;
                        fishMove(fish1, true);
                    }
                }
            }
        });

        mAnimatorSet = new AnimatorSet();
        mAnimatorSet.play(eatFishAnim);
        mAnimatorSet.start();
    }

    /**
     * @param isFirst true:从左边跳起    false：从右边跳起
     */
    void fishMove(final View view, boolean isFirst) {

        final float x = (int) view.getX();
        final float y = (int) view.getY();

        int h = fishLay.getHeight();
        int w = DisplayUtils.dp2px(this, 20);

        final AnimatorPath path = new AnimatorPath();
        path.moveTo(x, y);//初始化坐标
        if (isFirst) {
            path.curveTo((float) (x + 0.5 * w), y - h, x + 2 * w, y - h, x + 3 * w, y);//向右跳跃
        } else {
            path.curveTo((float) (x - 0.5 * w), y - h, x - 2 * w, y - h, x - 3 * w, y);//向左跳跃
        }
        ObjectAnimator anim = ObjectAnimator.ofObject(this, "fabLoc", new PathEvaluator(), path.getPoints().toArray());
        anim.setInterpolator(new AccelerateDecelerateInterpolator());
        anim.setDuration(800);
        anim.start();

        anim.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                PathPoint point = (PathPoint) animation.getAnimatedValue();
                view.setX(point.mX);
                view.setY(point.mY);
            }
        });

        anim.addListener(new Animator.AnimatorListener() {
            @Override
            public void onAnimationStart(Animator animation) {

            }

            @Override
            public void onAnimationEnd(Animator animation) {
                view.setX(x);
                view.setY(y);
            }

            @Override
            public void onAnimationCancel(Animator animation) {

            }

            @Override
            public void onAnimationRepeat(Animator animation) {

            }
        });
    }

}
